package com.tyj.gulimall.ware.service.impl;

import com.tyj.common.constant.WareConstant;
import com.tyj.gulimall.ware.entity.PurchaseDetailEntity;
import com.tyj.gulimall.ware.service.PurchaseDetailService;
import com.tyj.gulimall.ware.service.WareSkuService;
import com.tyj.gulimall.ware.vo.DonePurchaseVo;
import com.tyj.gulimall.ware.vo.ItemsVo;
import com.tyj.gulimall.ware.vo.MergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tyj.common.utils.PageUtils;
import com.tyj.common.utils.Query;

import com.tyj.gulimall.ware.dao.PurchaseDao;
import com.tyj.gulimall.ware.entity.PurchaseEntity;
import com.tyj.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService purchaseDetailService;

    @Autowired
    WareSkuService wareSkuService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        key: 1213
//        status: 1
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        String key= (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key).or().like("assignee_name",key).or().like("phone",key);
        }

        String status= (String) params.get("status");
        if (!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }



        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),wrapper

        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUnreceive(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        //TODO 确认采购需求是0或1才可以合并
        Long pruchaseId=mergeVo.getPurchaseId();
        //如果采购单等于null 否则需要创建一个采购单
        if (pruchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.ATTR_TYPE_BASE.getCode());
            this.save(purchaseEntity);
            pruchaseId=purchaseEntity.getId();
        }
        List<Long> items=mergeVo.getItems();
        Long finalPurchaseId=pruchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(i);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);

        //更新采购单
        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setId(pruchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }

    @Transactional
    @Override
    public void receivedPurchase(List<Long> purchaseId) {
        //根据purchaseId查询到 `wms_purchase_detail`对应的采购需求信息
        purchaseId.stream().forEach(item->{
            List<PurchaseDetailEntity> detailEntity= purchaseDetailService.queryByPurchaseId(item);
            //改变 `wms_purchase_detail`对应的采购需求信息的状态为 正在采购状态
            detailEntity.stream().forEach(item1->{
                item1.setStatus(WareConstant.PurchaseDetailStatusEnum.RECEIVE.getCode());
                purchaseDetailService.updateById(item1);
            });
            //改变采购单为以领取状态
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setId(item);
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            purchaseEntity.setUpdateTime(new Date());
            this.updateById(purchaseEntity);
        });
    }

    @Transactional
    @Override
    public void donePurchase(DonePurchaseVo donePurchaseVo) {
        //查询购物需求的购物情况
        List<ItemsVo> items = donePurchaseVo.getItems();

        Boolean flag=true;
        ArrayList<PurchaseDetailEntity> arrayList=new ArrayList<>();
        for (ItemsVo itemsVo:items) {
            PurchaseDetailEntity detailEntity=new PurchaseDetailEntity();
            if (itemsVo.getStatus()==4){
                flag=false;
            }else if (itemsVo.getStatus()==3){
                //采购成功 记录库存
                PurchaseDetailEntity purchaseDetailEntity = purchaseDetailService.getById(itemsVo.getItemId());
                wareSkuService.addWare(purchaseDetailEntity.getSkuId(),purchaseDetailEntity.getWareId(),purchaseDetailEntity.getSkuNum());

            }
            detailEntity.setId(itemsVo.getItemId());
            detailEntity.setStatus(itemsVo.getStatus());
            arrayList.add(detailEntity);
        }
        //批量修改采购需求
        purchaseDetailService.updateBatchById(arrayList);

        //根据flag标记判断采购是否完成 完成:购物单状态为3 未完成:购物单为4
        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setId(donePurchaseVo.getId());
        if (flag){
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISH.getCode());
        }else {
            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.HASERRO.getCode());
        }
        this.updateById(purchaseEntity);
    }


}